package com.volunteer.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.volunteer.common.Result;
import com.volunteer.entity.PointRecord;
import com.volunteer.service.PointService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 积分控制器
 */
@RestController
@RequestMapping("/api/points")
@RequiredArgsConstructor
public class PointController {

    private final PointService pointService;

    /**
     * 获取用户积分记录
     */
    @GetMapping("/user/{userId}")
    public Result<IPage<PointRecord>> getUserPointRecords(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        IPage<PointRecord> records = pointService.getUserPointRecords(userId, page, size);
        return Result.success(records);
    }

    /**
     * 手动调整积分（管理员）
     */
    @PostMapping("/adjust")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> adjustPoints(@RequestBody Map<String, Object> request) {
        Long userId = Long.parseLong(request.get("userId").toString());
        Integer points = Integer.parseInt(request.get("points").toString());
        String description = request.get("description").toString();
        pointService.adjustPoints(userId, points, description);
        return Result.success("积分调整成功", null);
    }

    /**
     * 获取用户总积分
     */
    @GetMapping("/user/{userId}/total")
    public Result<Integer> getUserTotalPoints(@PathVariable Long userId) {
        Integer totalPoints = pointService.getUserTotalPoints(userId);
        return Result.success(totalPoints);
    }
}

